﻿using System;
using System.Data;
using System.Data.SqlClient;
using DevExpress.XtraReports.UI;
using prjKQXS_DLL;
using prjKQXS_DLL.Hop_dong;

namespace prjKQXS_Report.Hop_dong
{
    public partial class rptInHDThang : DevExpress.XtraReports.UI.XtraReport
    {
        dsReports dsReports1 = new dsReports();

        //Cài đặt event
        public delegate void OnLoadCompleteHandler();
        public event OnLoadCompleteHandler OnLoadComplete;
       
        public rptInHDThang()
        {
            InitializeComponent();
        }

        private void rptInHDThang_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrlblTenDV.Text = "PHÒNG XSKT " + pTenDV.Value.ToString().Trim().ToUpper();
            xrlblCanBoQuanLy.Text = "Cán bộ quản lý: " + pTenCB.Value.ToString().Trim();
            xrlblNgay.Text = "Tháng " + ((DateTime)pNgay.Value).ToString("MM") + " năm " + ((DateTime)pNgay.Value).ToString("yyyy");

            Report_DAL report_dal = new Report_DAL();
            dsReports1 = report_dal.reportDungChung(dsReports1);

            clsHopDong cls = new clsHopDong();
            cls.OnAction += new clsHopDong.OnActionHandler(cls_OnAction);
            cls.OnError += new clsHopDong.OnErrorHandler(cls_OnError);

            dsReports1.Tables.Remove("rptBiendongdlCap2");
            int year = ((DateTime)pNgay.Value).Year;
            int month = ((DateTime)pNgay.Value).Month;
            DateTime ngay = new DateTime(year, month, DateTime.DaysInMonth(year, month));
            DataTable dt = cls.loadData_rptInHDThang(pMaDV.Value.ToString(), pDMCanBoID.Value.ToInt32(), ngay.Date);
            xuly(ref dt);
            dsReports1.Tables.Add(dt);
            this.DataSource = dsReports1;
            this.DataMember = "rptBiendongdlCap2";
        }

        int tong_DangHoatDong = 0, tong_DaThanhLy = 0;
        private void xuly(ref DataTable dt)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["Ngay_ThanhLy"] != DBNull.Value)
                {
                    DateTime ngayTL = (DateTime)row["Ngay_ThanhLy"];
                    int year = ((DateTime)pNgay.Value).Year;
                    int month = ((DateTime)pNgay.Value).Month;

                    if (ngayTL <= new DateTime(year, month, DateTime.DaysInMonth(year, month)))
                        tong_DaThanhLy++;
                    else tong_DangHoatDong++;

                    DateTime ngay = new DateTime(year, month, 1);
                    if (ngayTL.Month != ngay.Month || ngayTL.Year != ngay.Year) row["Ngay_ThanhLy"] = DBNull.Value;
                }
                else tong_DangHoatDong++;
            }
        }

        private void rptInHDThang_AfterPrint(object sender, EventArgs e)
        {
            OnLoadComplete();
        }

        int stt = 0; 
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrlblSTT.Text = (++stt).ToString();
            //if (GetCurrentColumnValue("Ngay_ThanhLy") != DBNull.Value) tong_DaThanhLy++;
            //else tong_DangHoatDong++;
        }

        #region cls event --------------------------------------------
        private void cls_OnAction(SqlException e)
        {
            cls_OnError(e);
        }

        private void cls_OnError(Exception e)
        {
            clsCommon.MsgBox("Một lỗi chưa được xử lý đã xảy ra. Chi tiết lỗi: \n" + e.Message, 0);
            this.CloseRibbonPreview();
        }
        #endregion ---------------------------------------------------

        private void ReportFooter_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrlblTongDangHD.Text = tong_DangHoatDong.ToString();
            xrlblTongThanhLy.Text = tong_DaThanhLy.ToString();
        }
    }
}
